#include <QtCore/QString>

#if defined(Q_OS_WIN)
#	include "xmppxmlparsertest_utf8_patch_win.h"
#else
#	include "xmppxmlparsertest_utf8_patch_unix.h"
#endif

#include "xmppxmlparsertest.h"
#include "xmppcore/xmppxmlparser.h"

#include <QtTest/QTest>

namespace yoohoo_xmppcore {

void XmppXmlParserTest::testParse() {
	XmppXmlParser parser;

	QString msgData1 = "<?xml version='1.0'?><stream:stream from='192.168.1.101' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='83101e51-3a23-4775-8388-c14a7877aa9b' version='1.0'><stream:features><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required/></starttls><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>DIGEST-MD5</mechanism><mechanism>PLAIN</mechanism></mechanisms></stream:features>";
	QString msgData2 = "<?xml version='1.0'?><stream:stream from='192.168.1.101' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='83101e51-3a23-4775-8388-c14a7877aa9b' version='1.0'><stream:features><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required/></starttls><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>DIGEST-MD5</mechanism><mech";
	QString msgData3 = "version='1.0'?><stream:stream from='192.168.1.101' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='83101e51-3a23-4775-8388-c14a7877aa9b' version='1.0'><stream:features><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required/></starttls><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>DIGEST-MD5</mechanism><mech";
	QString msgData4 = "<?xml version='1.0'?><stream:stream from='192.168.1.101' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='83101e51-3a23-4775-8388-c14a7877aa9b' version='1.0'><stream:features><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>DIGEST-MD5</mechanism><required/></starttls><mech";
	QString msgData5 = "<?xml version='1.0'><stream:stream from='192.168.1.101' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='83101e51-3a23-4775-8388-c14a7877aa9b' version='1.0'><stream:features><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required/></starttls><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>DIGEST-MD5</mechanism><mech";
	QString msgData6 = msgIncludesChinese;
	QString msgData7 = "<message to='romeo@example.net' from='juliet@example.com/balcony' type='chat' xml:lang='en'></stream:stream><?xml version='1.0'?><stream:stream from='192.168.1.101' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='83101e51-3a23-4775-8388-c14a7877aa9b' version='1.0'><stream:features>";
	QString msgData8 = "<stream:stream from='192.168.1.101' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='83101e51-3a23-4775-8388-c14a7877aa9b' version='1.0'>";
	QString msgData9 = "<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>";
	QString msgData10 = "<stream:error><host-unknown xmlns='urn:ietf:params:xml:ns:xmpp-streams'/></stream:error></stream:stream>";

	QList<QString> msgs1 = parser.parse(msgData1);
	QVERIFY(msgs1.size() == 3);
	QCOMPARE(msgs1.at(0), QString("<?xml version='1.0'?>"));
	QCOMPARE(msgs1.at(1), QString("<stream:stream from='192.168.1.101' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='83101e51-3a23-4775-8388-c14a7877aa9b' version='1.0'>"));
	QCOMPARE(msgs1.at(2), QString("<stream:features><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required/></starttls><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>DIGEST-MD5</mechanism><mechanism>PLAIN</mechanism></mechanisms></stream:features>"));
	QCOMPARE(msgData1, QString(""));

	QList<QString> msgs2 = parser.parse(msgData2);
	QVERIFY(msgs2.size() == 2);
	QCOMPARE(msgs2.at(0), QString("<?xml version='1.0'?>"));
	QCOMPARE(msgs2.at(1), QString("<stream:stream from='192.168.1.101' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='83101e51-3a23-4775-8388-c14a7877aa9b' version='1.0'>"));
	QCOMPARE(msgData2, QString("<stream:features><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required/></starttls><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>DIGEST-MD5</mechanism><mech"));

	QList<QString> msgs3 = parser.parse(msgData3);
	QVERIFY(msgs3.size() == 1);
	QCOMPARE(msgs3.at(0), QString("<stream:stream from='192.168.1.101' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='83101e51-3a23-4775-8388-c14a7877aa9b' version='1.0'>"));
	QCOMPARE(msgData3, QString("<stream:features><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required/></starttls><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>DIGEST-MD5</mechanism><mech"));

	QList<QString> msgs4 = parser.parse(msgData4);
	QVERIFY(msgs4.size() == 2);
	QCOMPARE(msgs4.at(0), QString("<?xml version='1.0'?>"));
	QCOMPARE(msgs4.at(1), QString("<stream:stream from='192.168.1.101' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='83101e51-3a23-4775-8388-c14a7877aa9b' version='1.0'>"));
	QCOMPARE(msgData4, QString("<mech"));

	QList<QString> msgs5 = parser.parse(msgData5);
	QVERIFY(msgs5.size() == 1);
	QCOMPARE(msgs5.at(0), QString("<stream:stream from='192.168.1.101' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='83101e51-3a23-4775-8388-c14a7877aa9b' version='1.0'>"));
	QCOMPARE(msgData5, QString("<stream:features><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required/></starttls><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>DIGEST-MD5</mechanism><mech"));

	QList<QString> msgs6 = parser.parse(msgData6);
	QVERIFY(msgs6.size() == 1);
	QCOMPARE(msgs6.at(0), msgIncludesChinese);
	QCOMPARE(msgData6, QString(""));

	QList<QString> msgs7 = parser.parse(msgData7);
	QVERIFY(msgs7.size() == 3);
	QCOMPARE(msgs7.at(0), QString("</stream:stream>"));
	QCOMPARE(msgs7.at(1), QString("<?xml version='1.0'?>"));
	QCOMPARE(msgs7.at(2), QString("<stream:stream from='192.168.1.101' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='83101e51-3a23-4775-8388-c14a7877aa9b' version='1.0'>"));
	QCOMPARE(msgData7, QString("<stream:features>"));

	QList<QString> msgs8 = parser.parse(msgData8);
	QVERIFY(msgs8.size() == 1);
	QCOMPARE(msgs8.at(0), QString("<stream:stream from='192.168.1.101' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='83101e51-3a23-4775-8388-c14a7877aa9b' version='1.0'>"));
	QCOMPARE(msgData8, QString(""));

	QList<QString> msgs9 = parser.parse(msgData9);
	QVERIFY(msgs9.size() == 1);
	QCOMPARE(msgs9.at(0), QString("<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"));
	QCOMPARE(msgData9, QString(""));

	QList<QString> msgs10 = parser.parse(msgData10);
	QCOMPARE(2, msgs10.size());
	QCOMPARE(msgs10.at(0), QString("<stream:error><host-unknown xmlns='urn:ietf:params:xml:ns:xmpp-streams'/></stream:error>"));
	QCOMPARE(msgs10.at(1), QString("</stream:stream>"));
	QCOMPARE(msgData10, QString(""));
}

}

QTEST_MAIN(yoohoo_xmppcore::XmppXmlParserTest);
